package com.itboye.jinpuyiyuan.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;

import java.util.List;

/**
 * Created by Administrator on 2017/12/23 0023.
 */

public class LocationInfoUtil {
    public static String locationProvider;

    //获取Location
    public static Location getLocation(Context context) {

        //获取地理位置管理器
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //获取所有可用的位置提供器
        List<String> providers = locationManager.getProviders(true);
        if (providers.contains(LocationManager.GPS_PROVIDER)) {
            //如果是GPS
            locationProvider = LocationManager.GPS_PROVIDER;
        } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
            //如果是Network
            locationProvider = LocationManager.NETWORK_PROVIDER;
        } else {
            Toast.makeText(context, "没有可用的位置提供器", Toast.LENGTH_SHORT).show();
        }
//        locationManager.getLastKnownLocation(locationProvider);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.

        }
        return locationManager.getLastKnownLocation(locationProvider);
    }
    //获取地理位置管理器
    public static LocationManager getLocationManager(Context context){

        return (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }

   //地理经度
   public static String getLongitude(Context context){
        if (LocationInfoUtil.getLocation(context)!=null){
            return LocationInfoUtil.getLocation(context).getLongitude()+"";
        }
       return "当前地理经度获取失败";
   }
    //地理纬度
    public static String getlatitude(Context context){
        if (LocationInfoUtil.getLocation(context)!=null){
            return LocationInfoUtil.getLocation(context).getLatitude()+"";
        }
        return "当前地理纬度获取失败";
    }

    //获取所有可用的位置提供器

    public static String getLocationProvider(Context context){
        LocationManager locationManager=LocationInfoUtil.getLocationManager(context);
        String locationProvider1;
        //获取所有可用的位置提供器
        List<String> providers = locationManager.getProviders(true);
        if (providers.contains(LocationManager.GPS_PROVIDER)) {
            //如果是GPS
            locationProvider1 = LocationManager.GPS_PROVIDER;
        } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
            //如果是Network
            locationProvider1 = LocationManager.NETWORK_PROVIDER;
        } else {
            Toast.makeText(context, "没有可用的位置提供器", Toast.LENGTH_SHORT).show();
            locationProvider1="";
        }
        return locationProvider1;
    }
}
